package cc.rengu.igas.channel.xmup.biz.impl;

import cc.rengu.igas.channel.xmup.common.constant.XmupServiceConstant;
import cc.rengu.igas.channel.xmup.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.xmup.facade.XmupCreditService;
import cc.rengu.igas.channel.xmup.facade.request.*;
import cc.rengu.igas.channel.xmup.facade.response.*;
import cc.rengu.igas.channel.xmup.facade.result.Result;
import cc.rengu.igas.channel.xmup.facade.response.CreditTransStateQueryRespone;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

public class XmupCreditServiceImpl implements XmupCreditService {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public Result<CreditTransStateQueryRespone> creditTransStateQuery(CreditTransStateQueryRequest creditTransStateQueryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<CreditTransStateQueryRespone> result = new Result<>();
        CreditTransStateQueryRespone creditTransStateQueryRespone = new CreditTransStateQueryRespone();
        try {
            DubboResult dubboResult = dubboService.callAtomService(creditTransStateQueryRequest,
                    XmupServiceConstant.CREDIT_TRANSTATE_QUERY_SERVICE, CreditTransStateQueryRespone.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((CreditTransStateQueryRespone) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("代付交易状态查询失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                creditTransStateQueryRespone.setRespDesc(((BizException) e).getCode());
                creditTransStateQueryRespone.setRespDesc(e.getMessage());
            } else {
                creditTransStateQueryRespone.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                creditTransStateQueryRespone.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<RepayResponse> singleRepay(RepayRequest repayRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<RepayResponse> result = new Result<>();
        RepayResponse repayResponse = new RepayResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(repayRequest,
                    XmupServiceConstant.SINGLE_REPAY_SERVICE, RepayResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((RepayResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("代付交易失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                repayResponse.setRespDesc(((BizException) e).getCode());
                repayResponse.setRespDesc(e.getMessage());
            } else {
                repayResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                repayResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

}
